/**
 *  example4.vala
 * 
 *  Copyright (c) 2007 INdT (Instituto Nokia de Technologia)
 *                2007 Marcelo Lira dos Santos <setanta@gmail.com>
 *
 *  Author: Marcelo Lira dos Santos <setanta@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA
 */

using GLib;
using Gtk;
using Hildon;
using Pango;

public enum TView {
    Thumbnail,
    Text,
    Image
}

public class Example4 : Hildon.Window
{
    [Widget] protected TreeView treeview;
    [Widget] protected ListStore store;
    [Widget] protected Image image;
    [Widget] protected Label label;

    public const int THUMB_WIDTH = 28;
    public const int IMG_HEIGHT = 360;

    construct {
        var hpaned = new HPaned();
        var vbox = new VBox(false, 4);

        image = new Image();
        vbox.pack_start(image, true, false, 0);

        label = new Label("...");
        vbox.pack_start(label, false, false, 0);

        ScrolledWindow scrolled_window = new ScrolledWindow(null, null);
        scrolled_window.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);

        treeview = new TreeView();
        store = new ListStore(3, typeof(Gdk.Pixbuf),
                                 typeof(string),
                                 typeof(Gdk.Pixbuf));
        treeview.set_model(store);
        treeview.row_activated += on_row_activated;

        var cell_pix = new CellRendererPixbuf();
        cell_pix.set_fixed_size(THUMB_WIDTH, -1);
        TreeViewColumn column1 = new TreeViewColumn.with_attributes(
            "Image", cell_pix, "pixbuf", TView.Thumbnail);
        treeview.append_column(column1);

        var cell_text = new CellRendererText();
        cell_text.ellipsize = EllipsizeMode.END;
        TreeViewColumn column2 = new TreeViewColumn.with_attributes(
            "Description", cell_text, "text", TView.Text);
        treeview.append_column(column2);

        scrolled_window.add_with_viewport(treeview);
        
        init_data();

        hpaned.pack1(scrolled_window, true, true);
        hpaned.pack2(vbox, true, true);
        hpaned.set_position(35);

        add (hpaned);

        destroy += Gtk.main_quit;
    }

    private void init_data() {
        Gdk.Pixbuf pixbuf1, pixbuf2;
        TreeIter treeIter;
        string dir = "data/";

        try {
            pixbuf1 = Gdk.Pixbuf.from_file_at_scale(
                dir + "raven4_dreary.jpg", THUMB_WIDTH, -1, true);
            pixbuf2 = Gdk.Pixbuf.from_file_at_scale(
                dir + "raven4_dreary.jpg", -1, IMG_HEIGHT, true);
            store.append(ref treeIter);
            store.set(ref treeIter,
                      TView.Thumbnail, pixbuf1,
                      TView.Text, "A Midnight Dreary",
                      TView.Image, pixbuf2);
            
            pixbuf1 = Gdk.Pixbuf.from_file_at_scale(
                dir + "raven10_nothingmore.jpg", THUMB_WIDTH, -1, true);
            pixbuf2 = Gdk.Pixbuf.from_file_at_scale(
                dir + "raven10_nothingmore.jpg", -1, IMG_HEIGHT, true);
            store.append(ref treeIter);
            store.set(ref treeIter,
                      TView.Thumbnail, pixbuf1, 
                      TView.Text, "Darkness there and nothing more",
                      TView.Image, pixbuf2);
            
            pixbuf1 = Gdk.Pixbuf.from_file_at_scale(
                dir + "raven26_soul.jpg", THUMB_WIDTH, -1, true);
            pixbuf2 = Gdk.Pixbuf.from_file_at_scale(
                dir + "raven26_soul.jpg", -1, IMG_HEIGHT, true);
            store.append(ref treeIter);
            store.set(ref treeIter,
                      TView.Thumbnail, pixbuf1,
                      TView.Text, "My Soul from out that Shadow",
                      TView.Image, pixbuf2);
                
        } catch (GLib.Error error) {
            critical(error.message);
        }
        
    }

    private void on_row_activated (TreeView tview, TreePath path, TreeViewColumn column)
    {
        TreeIter iter;
        string desc;
        Gdk.Pixbuf pixbuf;
        
        store.get_iter(ref iter, path);
        store.get(ref iter, TView.Text, ref desc);
        store.get(ref iter, TView.Image, ref pixbuf);

        image.set_from_pixbuf(pixbuf);
        label.set_text(desc);
    }

    static int main (string[] args) {
        Gtk.init(out args);

        Environment.set_application_name("Sample 4");

        Example4 ex4 = new Example4();

        Hildon.Program program = Hildon.Program.get_instance();
        program.add_window((Hildon.Window) ex4);

        ex4.show_all();

        Gtk.main();

        return 0;
    }
}
